#include "tinyrpc/base/coroutine/coroutine.h"
#include <string>
#include <iostream>
#include "tinyrpc/base/thread.h"
#include "tinyrpc/base/task.h"
#include "tinyrpc/base/future.hpp"
#include <unistd.h>
using namespace TinyRPC;

void task1(){
    std::cout<<"in coroutine1"<<std::endl;
    Coroutine::Yield();
    std::cout<<"in coroutine2"<<std::endl;
}

// 在线程内调用也正常
void task2(){
    std::cout<<"in thread coroutine1"<<std::endl;
    Coroutine::Yield();
    std::cout<<"in  thread coroutine2"<<std::endl;
}

int task3(){
    std::cout<<"in  thread coroutine3"<<std::endl;
    return 1;
}   

int main(){
    Task t1(task1, true);
    // 这里有缺点, 无法判断协程是否已经跑完了， 之后继续加入
    t1.run();
    std::cout<<"in main"<<std::endl;
    t1.run();

    Task t2(task2, true);
    Thread thread1([&](){
        t2.run();
        t2.run();
    });
    thread1.start();
    thread1.join();

    Future<int> future;
    //Task::TaskFuncWithReturn<int> func1 = task3;
    Task t3 = Task::AsyncTask<int>(task3, future);

    Thread thread2([&](){
        sleep(3);
        t3.run();
    
    });
    thread2.start();
    std::cout<<future.get()<<std::endl;
    thread2.join();

}